package org.csu.gp_store.controller;

import org.csu.gp_store.bo.CartItemBO;
import org.csu.gp_store.common.CommonResponse;
import org.csu.gp_store.entity.CartItem;
import org.csu.gp_store.service.CartItemService;
import org.csu.gp_store.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;


@RestController
@RequestMapping("/carts")
public class CartController {

    @Autowired
    private CartItemService cartItemService;

    @GetMapping("/cart")
    @ResponseBody
    public CommonResponse<List<CartItemBO>> getCartProductListByAccountID(@RequestHeader("MyToken") String MyToken) {

        if (null ==MyToken ||"".equals(MyToken)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }
        String accountID = TokenUtil.getUserIdByToken(MyToken);
        if (null==accountID ||"".equals(accountID)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }
        return CommonResponse.createForSuccess(cartItemService.getCartItemBOListByAccountID(Long.valueOf(accountID)));
    }



    @PatchMapping("/cart/quantity")
    @ResponseBody
    public CommonResponse<String> updateCartItemQuantity(@RequestHeader("MyToken") String MyToken,
                                                         @RequestParam("cartID") String cartID,
                                                         @RequestParam("quantity") String quantity) {

        if (null ==MyToken ||"".equals(MyToken)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }
        try{
            if(cartItemService.updateCartItemQuantity(Long.valueOf(cartID),Integer.parseInt(quantity))==1) {
                return CommonResponse.createForSuccessMsg("修改成功");
            }else {
                return CommonResponse.createForSuccessMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonResponse.createForError("出现未知错误，请稍后重试");
    }

    @PatchMapping("/cart/quantity/one")
    @ResponseBody
    public CommonResponse<String> updateCartProductQuantityAddOne( @RequestHeader("MyToken") String MyToken,
                                                                   @RequestParam("cartID") String cartID) {

        if (null ==MyToken ||"".equals(MyToken)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }

        try{
            if(cartItemService.updateCartItemQuantityAddOne(Long.valueOf(cartID))==1){
                return CommonResponse.createForSuccessMsg("修改成功");
            }else {
                return CommonResponse.createForSuccessMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonResponse.createForError("出现未知错误，请稍后重试");
    }

    @DeleteMapping("/cart")
    @ResponseBody
    public CommonResponse<String> deleteProductFromCart(@RequestHeader("MyToken") String MyToken,
                                                        @RequestParam("cartID") String cartID) {

        if (null ==MyToken ||"".equals(MyToken)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }
        try{
            if(cartItemService.deleteItemFromCart(Long.valueOf(cartID))==1){
                return CommonResponse.createForSuccessMsg("删除成功");
            }else {
                return CommonResponse.createForSuccessMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonResponse.createForError("出现未知错误，请稍后重试");
    }

    @PostMapping("/cart")
    CommonResponse<String> addItemToCartProduct(@RequestHeader("MyToken") String MyToken,
                                                @RequestParam("itemID") String itemID,
                                                @RequestParam("quantity") String quantity) {

        if (null ==MyToken ||"".equals(MyToken)){
            return CommonResponse.createForError("身份验证失败，请登陆后再试！");
        }

        try {
            CartItem cartItem = new CartItem();
            String accountID=TokenUtil.getUserIdByToken(MyToken);
            if (null==accountID ||"".equals(accountID)){
                return CommonResponse.createForError("身份验证失败，请登陆后再试！");
            }
            cartItem.setAccountID(Long.valueOf(accountID));
            cartItem.setItemID(Long.valueOf(itemID));
            cartItem.setQuantity(Integer.parseInt(quantity));
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            cartItem.setAddTime(timestamp);
            if (cartItemService.addItemToCartItem(cartItem)==1){
                return CommonResponse.createForSuccessMsg("添加商品到购物车成功");
            }else {
                return CommonResponse.createForSuccessMsg("添加商品到购物车失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            return CommonResponse.createForError("出现未知错误，请稍后重试。");
    }

}
